package org.example.controller;

import lombok.extern.slf4j.Slf4j;
import org.example.pojo.Food;
import org.example.pojo.Result;
import org.example.service.FoodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping
public class FoodController {
    @Autowired
    private FoodService foodService;

    /*
    * 新增食物
    * */
    @PostMapping("/addFood")
    public Result addFood(@RequestBody Food food) {
        log.info("新增食物：{}", food);
        foodService.addFood(food);
        return Result.success();
    }

    /*
    * 查询所有食物
    * */
    @GetMapping("/foods")
    public Result getAllFoods() {
        log.info("查询所有食物");
        List<Food> foods = foodService.getAllFoods();
        return Result.success(foods);
    }

    /*
    * 根据ID查询食物
    * */
    @GetMapping("/foods/{id}")
    public Result getFoodById(@PathVariable Integer id) {
        log.info("根据ID查询食物：{}", id);
        Food food = foodService.getFoodById(id);
        return Result.success(food);
    }

    /*
    * 删除食物
    * */
    @DeleteMapping("/foods/{id}")
    public Result deleteFood(@PathVariable Integer id) {
        log.info("删除食物：{}", id);
        foodService.deleteFood(id);
        return Result.success();
    }

    /*
    * 更新食物
    * */
    @PutMapping("/foods/{id}")
    public Result updateFood(@PathVariable Integer id, @RequestBody Food food) {
        log.info("更新食物：{}", food);
        foodService.updateFood(id, food);
        return Result.success();
    }
}
